local require = require
local uu     = require("modules.commhm.utils")
local config = require("modules.business_common.business_config")
local http   = require("modules.business_common.utils.business_http")

local ngx = ngx
local curr = os.time

local table = table
local pairs = pairs
local ipairs = ipairs
local tonumber = tonumber
local tostring = tostring
local loadstring = table.loadstring

local FOO = {
    _VERSION = '0.0.1'
}

local http_request = function( uin, request )
    if not (request and request.cmd) then
        return nil
    end

    request.time = curr()
    request.auth = ngx.md5 ( request.time .. '#miniw_907#'  .. (tostring(uin) or '') )

    local server = config.get_ma_server_url(uin)
    local response = loadstring(http.Get(server, request) or 'nil')
    if response and tonumber(response.ret) == 0 then
        return true, response
    end

    -- 失败日志
    http.Log(uu.to_str({ server = server, request = request, response = response }))
    return false
end

FOO.report_task = function (request, uin)
    uin = uin or ngx.ctx.m_params.uin

    request.cmd = 'set_task_data'
    request.uin = uin
    return http_request(uin, request)
end

return FOO